﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Touch2Like.Model;
using System.Data;

namespace Touch2Like.DAL
{
    public class KnowledgeDAL
    {
        #region Add
        public int Add(KnowledgeInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeOutParam("@ListID",SqlDbType.Int,4),
                db.MakeInParam("@Title",SqlDbType.VarChar,500, info.Title),
                db.MakeInParam("@Content",SqlDbType.VarChar,5000,info.Content)
            };
            try
            {
                db.ExecNonQuery("CAM_Knowledge_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Update
        public void Update(KnowledgeInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,info.ListID),
                db.MakeInParam("@Title",SqlDbType.VarChar,500, info.Title),
                db.MakeInParam("@Content",SqlDbType.VarChar,5000,info.Content)
            };
            try
            {
                db.ExecNonQuery("CAM_Knowledge_Update", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Delete
        public void Delete(int listID)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,listID)
            };
            try
            {
                db.ExecNonQuery("CAM_Knowledge_Delete", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get
        public KnowledgeInfo GetByID(int listID)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
                {
                   db.MakeInParam("@ListID",SqlDbType.Int,4, listID)
                };

            try
            {
                dr = db.RunProc("CAM_Knowledge_GetByID", param);

                if (dr.Read())
                {
                    KnowledgeInfo info = new KnowledgeInfo();

                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.Title = DataHelper.GetString(dr["Title"]);
                    info.Content = DataHelper.GetString(dr["Content"]);
                    info.Void = DataHelper.GetInt(dr["Void"]);
                    info.UpdateDate = DataHelper.GetDateTime(dr["UpdateDate"]);

                    return info;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion


        #region Get By Random
        public KnowledgeInfo GetByRandom()
        {
            Database db = new Database();
            SqlDataReader dr = null;

            try
            {
                dr = db.RunProc("CAM_Knowledge_GetByRandom");

                if (dr.Read())
                {
                    KnowledgeInfo info = new KnowledgeInfo();

                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.Title = DataHelper.GetString(dr["Title"]);
                    info.Content = DataHelper.GetString(dr["Content"]);
                    info.Void = DataHelper.GetInt(dr["Void"]);
                    info.UpdateDate = DataHelper.GetDateTime(dr["UpdateDate"]);

                    return info;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion

        #region Get By Customer
        public DataTable GetAll()
        {
            Database db = new Database();

            try
            {
                return db.GetDataSet("CAM_Knowledge_GetAll").Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion
    }
}
